package com.totoro.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.totoro.domain.Project;
import com.totoro.domain.Staff;
import com.totoro.domain.Tutor;
import com.totoro.service.ProjectService;
import com.totoro.service.StaffService;
import com.totoro.service.TutorService;
import com.totoro.util.CommonConstant;
import com.totoro.util.Page;

@Controller
@RequestMapping("/proposer")
public class ProjectController {

	@Autowired
	private ProjectService projectService;

	@Autowired
	private TutorService tutorService;
	
	@RequestMapping("/list")
	public ModelAndView list(Integer pageNo,Authentication authentication) {
		System.out.println("/proposer/list");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/proposer/list");						  
		
		
		Tutor tutor = tutorService.uniqueResult("no", authentication.getName());
		
		
		pageNo = pageNo == null ? 1 : pageNo;
		Page<Project> pagedPost = projectService.getPagePostsByTutor(pageNo,
				CommonConstant.PAGE_SIZE,tutor);
		
		mav.addObject("pagedPost", pagedPost);
		mav.addObject("projectList", pagedPost.getResult());
		mav.addObject("pageNo", pageNo);
		return mav;
	}

	@RequestMapping("/findByName")
	public ModelAndView findByName(String name) {
		System.out.println("description: " + name);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/staff/list");
		mav.addObject("staffList",
				projectService.list("name", new String(name)));
		return mav;
	}

	@RequestMapping(value = "/saveOrUpdate", method = RequestMethod.GET)
	public String toSave(ModelMap modelMap) {	
		modelMap.addAttribute("staff",new Staff());
		return "/staff/saveOrUpdate";
	}
//	保存或更新
	@RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
	public String saveOrUpdate(@ModelAttribute("project") Project project) {
		System.out.println("staff: " + project);
//		如果id为null，则为save，否则update
		projectService.saveOrUpdate(project);
		return "redirect:/proposer/list.html";
	}

	@RequestMapping("/findById/{id}")
	public String findById(@PathVariable("id") Integer id, ModelMap modelMap) {
		modelMap.addAttribute("staff", projectService.get(id));
		return "/staff/saveOrUpdate";
	}

//	@RequestMapping("/update")
//	public String update(@ModelAttribute("staff") Staff staff) {
//		System.out.println(staff);
//		projectService.update(staff);
//		return "redirect:/staff/list.html";
//	}

	@RequestMapping("/delete/{id}")
	public String delete(Project project) {
		projectService.deleteObject(project);
		return "redirect:/staff/list.html";
	}
	
	//增加评语模板json视图
	@RequestMapping("/findById")
	public String findByIdToJson(Integer id,ModelMap model){
		System.out.println("id的值为：" + id);
		Project project = projectService.get(id);
		System.out.println("describtion = " + project.getTitle());
		model.addAttribute("describtion", project.getTitle());
		return "staffJson";
	}

}
